﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using HardwareInterface;
using System.Threading;

namespace HardwareInterface
{
    class FingerScanApp
    {
        public void ReleaseDevice()
        {
            FingerScan.CloseDevice();
        }
        public int InitDevice()
        {
            if (FingerScan.OpenDevice(0, 0, 0) == 1)
            {
                if (FingerScan.LinkDevice() == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return -1;
            }
        }
        public int RegisterFinger_Part1(ref Bitmap bmp1)
        {
            FingerScan.EnrolFpChar();
            bmp1 = LoadOneFinger();
            return 1;
        }
        public int RegisterFinger_Part2(ref byte[] result, ref int resultsize, ref Bitmap bmp2)
        {
            bmp2 = LoadOneFinger();
            GetMessage();
            int cnt = 5;
            while (retMsg != 1) //register ok
            {
                cnt--;
                if (cnt < 0)
                    return 0;
                GetMessage();
            }
            FingerScan.GetFpCharByEnl(ref result[0], ref resultsize);
            return 1;
        }

        public int GetLoginFinger(ref byte[] result, ref int size, ref Bitmap bmp)
        {
            FingerScan.GenFpChar();
            bmp = LoadOneFinger();
            GetMessage();
            int cnt = 5;
            while (retMsg != 1) //register ok
            {
                cnt--;
                if (cnt < 0)
                    return 0;
                GetMessage();
            }
            FingerScan.GetFpCharByGen(ref result[0], ref size);
            return 1;
        }

        public bool Match(byte[] template, byte[] input)
        {
            int scope = FingerScan.MatchTemplate(ref template[0], ref input[0]);
            scope = scope + FingerScan.MatchTemplate(ref template[256], ref input[0]);
            return scope > 30;
        }

        private Bitmap LoadOneFinger()
        {
            GetMessage();
            while (workMsg != FingerScan.FPM_PLACE)
                GetMessage();
            GetMessage();
            while (workMsg != FingerScan.FPM_LIFT)
                GetMessage();
            GetMessage();
            while (workMsg != FingerScan.FPM_NEWIMAGE)
                GetMessage();
            Bitmap bmp = new Bitmap(255, 288);
            Graphics g = Graphics.FromImage(bmp);
            FingerScan.DrawImage(g.GetHdc(), 0, 0);
            g.Dispose();
            return bmp;
        }
        private void GetMessage()
        {
            Thread.Sleep(200);
            workMsg = FingerScan.GetWorkMsg();
            retMsg = FingerScan.GetRetMsg();
        }
        int state;
        int workMsg;
        int retMsg;
    }
}
